package com.project.service;

import com.project.dao.MajorMapper;
import com.project.entity.Major;
import com.project.service.base.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by WheelChen on 2017/5/4.
 */
@Transactional(rollbackFor=Exception.class)
@Service
public class MajorServiceImpl extends BaseService implements MajorService {
    private static MajorMapper majorMapper=null;

    public void setMapper()
    {
        if(majorMapper==null)
            majorMapper = sqlSessionTemplate.getMapper(MajorMapper.class);//1.获取相关mapper
    }
    @Override
    public int addOne(Major major) {
        setMapper();
        return majorMapper.insertSelective(major);
    }

    @Override
    public int remove(Integer id) {
        setMapper();
        return majorMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int modify(Major major) {
        setMapper();
        return majorMapper.updateByPrimaryKeySelective(major);
    }

    @Override
    public Major findOne(Integer id) {
        setMapper();
        return majorMapper.selectByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017-6-22
     * function: 根据系id寻找专业
     *
     * @param departmentId 系id
     * @return
     */
    @Override
    public List<Major> findBatch(Integer departmentId) {
        setMapper();
        return majorMapper.selectByDepartmentId(departmentId);
    }

    /**
     * author: WheelChen
     * date: 2017/8/10
     * function: 检查专业是否已存在
     *
     * @param major
     * @return true - 存在 false - 不存在
     */
    @Override
    public boolean checkMajorIsExist(Major major) {
        setMapper();
        return majorMapper.checkMajorIsExist(major) != 0;
    }
}
